<template>
    <div>
        <!--对数据做查询-->
        <el-row>
            <el-form :inline="true" :model="formInline" ref="formInline" class="demo-form-inline">
                <el-form-item label="字典ID">
                    <el-input v-model="formInline.dictCode" placeholder="字典ID"></el-input>
                </el-form-item>
                <el-form-item label="字典名称">
                    <el-input v-model="formInline.dictLabel" placeholder="字典名称"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="onSubmit">查询</el-button>
                    <el-button @click="resetForm">重置</el-button>
                </el-form-item>
                <el-button
                        type="primary"
                        @click="handleAdd">添加用户
                </el-button>
            </el-form>
        </el-row>
        <!--表格内容-->
        <el-row>
            <el-table
                    :data="tableData"
                    style="width: 100%" border>
                <el-table-column
                        prop="dictCode"
                        label="字典ID">
                </el-table-column>
                <el-table-column
                        prop="dictLabel"
                        label="字典名称">
                </el-table-column>
                <el-table-column
                        prop="dictValue"
                        label="字典键值">
                </el-table-column>
                <el-table-column
                        prop="dictType"
                        label="字典类型">
                </el-table-column>
                <el-table-column
                        prop="listClass"
                        label="表格回显样式">
                </el-table-column>
                <el-table-column
                        prop="isDefault"
                        label="是否默认">
                </el-table-column>
                <el-table-column
                        prop="status"
                        label="菜单状态"
                        :formatter="status">
                </el-table-column>
                <el-table-column
                        prop="createBy"
                        label="创建人">
                </el-table-column>
                <el-table-column
                        prop="createTime"
                        label="创建日期">
                </el-table-column>
                <el-table-column
                        prop="updateBy"
                        label="更新者">
                </el-table-column>
                <el-table-column
                        prop="updateTime"
                        label="更新时间">
                </el-table-column>
                <el-table-column
                        prop="remark"
                        label="备注">
                </el-table-column>
                <el-table-column label="操作" width="280">
                    <template slot-scope="scope">
                        <el-button
                                size="mini"
                                @click="handleEdit(scope.$index, scope.row)">编辑
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-row>
        <!--分页定义-->
        <el-row>
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="currentPage"
                    :page-sizes="[3,5,10,20]"
                    :page-size="pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
            </el-pagination>
        </el-row>

        <!--编辑或添加对话框-->
        <el-dialog
                :title="title"
                :visible.sync="addDialogVisible"
                width="30%">
            <el-form :model="editOrAddForm" ref="editOrAddForm" :rules="rules" label-width="50px"
                     class="demo-ruleForm">
                <el-input v-model="editOrAddForm.dictCode" style="display: none"></el-input>
                <el-form-item label="名称" prop="dictLabel">
                    <el-input v-model="editOrAddForm.dictLabel"></el-input>
                </el-form-item>
                <el-form-item label="键值" prop="dictValue">
                    <el-input v-model="editOrAddForm.dictValue"></el-input>
                </el-form-item>
                <el-form-item label="字典类型" prop="dictType">
                    <el-input v-model="editOrAddForm.dictType"></el-input>
                </el-form-item>
                <el-form-item label="格式" prop="listClass">
                    <el-input v-model="editOrAddForm.listClass"></el-input>
                </el-form-item>
                <el-form-item label="默认" prop="isDefault">
                    <el-radio-group v-model="editOrAddForm.isDefault">
                        <el-radio :label="`Y`" border>是</el-radio>
                        <el-radio :label="`N`" border>否</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="状态" prop="status">
                    <el-radio-group v-model="editOrAddForm.status">
                        <el-radio :label="`0`" border>正常</el-radio>
                        <el-radio :label="`1`" border>禁用</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="备注">
                    <el-input v-model="editOrAddForm.remark"></el-input>
                </el-form-item>
            </el-form>
            <el-button @click="closeEditOrAddDialog">取 消</el-button>
            <el-button type="primary" @click="submitEditOrAddDialog('editOrAddForm')">确 定</el-button>
        </el-dialog>
    </div>
</template>
<script>
    export default {
        data() {
            return {
                tableData: [],
                currentPage: 1,//当前页
                total: 100, //总数据量
                formInline: {dictCode: '', dictLabel: ''},
                pageSize: 5,//每页数据量
                addDialogVisible: false, //对话框的状态，默认关闭
                title: '',
                editOrAddForm: {
                    dictCode: '',
                    dictLabel: '',
                    dictValue: '',
                    dictType: '',
                    listClass: '',
                    isDefault: '',
                    status: '',
                    remark: '',
                },
                rules: {
                    dictLabel: [
                        {required: true, message: '请输入字典名称', trigger: 'blur'},
                    ],
                    dictValue: [
                        {required: true, message: '请输入字典键值', trigger: 'blur'},
                    ],
                    status: [
                        {required: true, message: '请选择状态', trigger: 'blur'},
                    ],
                    isDefault: [
                        {required: true, message: '请选择是否默认', trigger: 'blur'},
                    ],
                },
            }
        },
        // 初始化数据  mounted()
        created: function () {
            this.loadData();
        },
        methods: {
            //封装数据请求
            loadData() {
                var _this = this;
                this.$http.post("/ss/dictData/queryAll?pageNo=" + this.currentPage + "&pageSize=" + this.pageSize, this.formInline).then((response) => {
                    _this.tableData = response.data.t.list;
                    _this.total = response.data.t.total;

                })
            },
            //处理编辑
            async handleEdit(index, row) {
                let rowData = JSON.parse(JSON.stringify(row));
                this.editOrAddForm = {};
                this.title = '编辑字典信息';
                this.editOrAddForm = rowData;
                this.addDialogVisible = true;
            },
            //处理删除
            deleteUser(index, row) {

            },
            //处理添加
            handleAdd() {
                this.editOrAddForm = {};
                this.title = '添加字典信息';
                this.addDialogVisible = true;
            },

            //分页 每页数量变化时处理
            handleSizeChange(val) {
                this.pageSize = val;
                this.loadData();
            },
            //分页 第几页变化时处理
            handleCurrentChange(val) {
                this.currentPage = val;
                this.loadData();
            },
            //点击查询方法
            onSubmit() {
                this.currentPage = 1;
                this.loadData();
            },
            //判断角色状态
            status(row) {
                if (row.status === "0") {
                    return "正常"
                } else {
                    return "禁用"
                }
            },
            //重置表单
            resetForm() {
                this.formInline = {};
                this.loadData();
            },closeEditOrAddDialog() {
                this.addDialogVisible = false;
            },
            //编辑和添加提交
            submitEditOrAddDialog(editOrAddForm) {
                this.$refs[editOrAddForm].validate((valid) => {
                    if (valid) {
                        if (this.editOrAddForm.dictCode >= 1) {
                            this.$http.put("/ss/dictData/update", this.editOrAddForm).then((response) => {
                                if (response.data.resultCode === 2000) {
                                    this.$message({
                                        message: '修改成功！',
                                        type: 'success'
                                    });
                                    this.loadData();
                                    this.addDialogVisible = false;
                                } else {
                                    this.$message({
                                        message: response.data.resultMessage,
                                        type: 'error'
                                    });
                                }
                            })
                        } else {
                            this.$http.post("/ss/dictData/insert", this.editOrAddForm).then((response) => {
                                if (response.data.resultCode === 2000) {
                                    this.$message({
                                        message: '添加成功！',
                                        type: 'success'
                                    });
                                    this.loadData();
                                    this.addDialogVisible = false;
                                } else {
                                    this.$message({
                                        message: response.data.resultMessage,
                                        type: 'error'
                                    });
                                }
                            })
                        }
                    } else {
                        return false;
                    }
                });
            },

        }
    }
</script>
<style>

</style>